package cn.wsalix.biz.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.admin.BaseUser;
import cn.anlaser.form.UploadFileForm;
import cn.anlaser.service.impl.CommonServiceImpl;
import cn.anlaser.status.FlagEnum;
import cn.anlaser.type.FileTypeEnum;
import cn.anlaser.type.ImageTypeEnum;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.biz.entity.Article;
import cn.wsalix.biz.entity.Category;
import cn.wsalix.biz.entity.RelationCat;
import cn.wsalix.biz.form.ArticleForm;
import cn.wsalix.biz.repository.ArticleRepository;
import cn.wsalix.biz.repository.RelationCatRepository;
import cn.wsalix.biz.service.ArticleService;
import cn.wsalix.biz.type.CatTypeEnum;
import cn.wsalix.observable.service.ArtChangeService;
import cn.wsalix.observable.service.FileUploadService;
import cn.wsalix.spring.SpringUtils;
import cn.wsalix.status.ActEnum;
import cn.wsalix.utils.UserUtils;

@Service("articleService")
public class ArticleServiceImpl extends
		CommonServiceImpl<ArticleForm, ArticleRepository, Article> implements
		ArticleService {
	@Autowired
	private ArticleRepository articleRepository;
	@Autowired
	private RelationCatRepository relationCatRepository;
	@Autowired
	private SpringUtils springUtils;
	@Autowired
	protected UserUtils userUtils;

	@Override
	public String module() {
		return "/biz/article";
	}

	@Transactional
	@Override
	public Article getNew() {
		BaseUser user = userUtils.getUser();
		ArticleForm form = new ArticleForm();
		form.setFlag(FlagEnum.create);
		form.setCreatedBy(user.getId());
		List<Article> resLst = findByForm(form);
		if (!resLst.isEmpty()) {
			return resLst.get(0);
		} else {
			Article entity = new Article();
			entity.setFlag(FlagEnum.create);
			entity.setCreatedBy(user.getId());
			entity.setCreatedDate(new Date());
			return add(entity);
		}
	}

	@Override
	public Article add(ArticleForm form) {
		Article obj = articleRepository.findOne(form.getId());
		traversalAssign(form, obj);
		obj.setFlag(FlagEnum.valid);
		return add(obj);
	}

	@Override
	public ArticleRepository getRepository() {
		return articleRepository;
	}

	@Override
	public Class<Article> getEntityClazz() {
		return Article.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(ArticleForm form) {
		Map<String, SearchFilter> filters = buildFilter(form);

		if (form.getCatId() != null && form.getCatId() != 0) {
			filters.put("category.id", new SearchFilter("category.id",
					Operator.EQ, form.getCatId()));
		}

		if (form.getStatus() != null) {
			filters.put("status", new SearchFilter("status", Operator.EQ, form
					.getStatus().ordinal()));
		}

		return filters;
	}

	@Override
	public List<Article> findByCatoryId(Long catoryId) {
		ArticleForm form = new ArticleForm();
		form.setCatId(catoryId);
		form.setStatus(ActEnum.release);
		return findByForm(form);

	}

	@Transactional
	@Override
	public void delete(Long id) {
		// TODO 已发布不允许删除
		super.delete(id);
	}

	@Transactional
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof FileUploadService) {
			FileUploadService fileUploadService = (FileUploadService) o;
			if (arg instanceof UploadFileForm) {
				UploadFileForm obj = (UploadFileForm) arg;
				if (obj.getType().ordinal() == FileTypeEnum.image.ordinal()) {
					if (obj.getImageType().equals( ImageTypeEnum.article.name())) {// 用户头像
						if (obj.getFkId() != 0) {
							Article ad = articleRepository.findOne(obj
									.getFkId());
							ad.setLogo(obj.getHttpUrl());
							super.edit(ad);
						}
					}
				}
			}
		}
		if (o instanceof ArtChangeService) {
			ArtChangeService artChangeService = (ArtChangeService) o;
			if (arg instanceof ArticleForm) {
				ArticleForm form = (ArticleForm) arg;
				if (form.isDelete()) {
					delete(form.getId());
				}
				Article article;
				if (form.getId() == null || form.getId() == 0) {// 用户头像
					form.setStatus(ActEnum.edit);
					article = add(form);
					/*
					 * if(form.getFkId() == null || form.getId() == 0){
					 * ChangeFk(form,article); }
					 */
				} else {
					article = edit(form);
				}
				artChangeService.setArticle(article);
			}
		}
	}

	/*
	 * @Transactional private void ChangeFk(ArticleForm form, Article article) {
	 * if (form.getType().ordinal() == ArticleEnum.ad.ordinal()) { AdArticle
	 * adArticleRepository.save(entity) } }
	 */

	@Transactional
	@Override
	public void addCat(Long adId, Category category) {
		RelationCat relationCat = new RelationCat();
		relationCat.setCategory(category);
		relationCat.setFkId(adId);
		relationCat.setCatType(CatTypeEnum.article);
		relationCatRepository.save(relationCat);
	}

	@Transactional
	@Override
	public void delCat(Long artId, Long catId) {
		relationCatRepository.deleteByCatTypeAndFkid(artId, catId,
				CatTypeEnum.article);
	}
}
